package com.service;

import com.dao.OrderItemMapper;
import com.pojo.Order;
import com.pojo.OrderItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderItemsService {

    @Autowired
    private OrderItemMapper orderItemMapper;


    //删除订单明细的条目，默认只删除一条,只按照oiId删除
    public void deleteOrderItems(Integer oiId){
        orderItemMapper.deleteByPrimaryKey(oiId);
    }

    //编辑订单明细的条目,并非批量更改，单个修改
    public void updateOrderItems(OrderItem orderItem){
        orderItemMapper.updateByPrimaryKeySelective(orderItem);
    }

    //根据外键的订单编号，删除订单明细的条目
    public void deleteOrderItemsByOrderId(Integer oId){
        orderItemMapper.deleteItemsByOrderId(oId);
    }

    //逐条插入明细表,购物车提交的时候插入
    public void insertOrderItems(OrderItem orderItem){
        orderItemMapper.insertSelective(orderItem);
    }


    //批量插入订单明细表
    public void insertOrderItemsAll(Integer oId ,List<OrderItem> orderItemList){
        if(orderItemList!=null && orderItemList.size()!=0 && oId!=null){
            for(OrderItem orderItem: orderItemList){
                orderItem.setoId(oId);  //设置订单id
                orderItemMapper.insertSelective(orderItem);
            }
        }
    }


    //计算小计金额
    public void calAllSubTotalMoney(List<OrderItem> orderItemList){
        for(OrderItem orderItem: orderItemList){
            //bigdecimal * int => 把int 转成decimal在相乘
            orderItem.setOiSubtotal( orderItem.getDish().getPrice().multiply(new BigDecimal(orderItem.getNum())));
        }
    }

}
